<template>
  <div class="announcement-management">
    <el-card class="rounded-3xl">
      <template #header>
        <div class="card-header">
          <span>编写公告</span>
        </div>
      </template>
      <div class="p-8">
        <el-form :model="announcement" label-width="5rem">
          <el-form-item label="公告名称">
            <el-input
              v-model="announcement.name"
              placeholder="请输入公告名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="公告类型">
            <el-select v-model="announcement.type" placeholder="请选择公告类型">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
              <!-- 可以添加更多公告类型 -->
            </el-select>
          </el-form-item>
          <el-form-item label="公告展示位">
            <el-radio-group v-model="announcement.display">
              <el-radio label="首页公告区">首页公告区</el-radio>
              <!-- 可以添加更多展示位选项 -->
            </el-radio-group>
          </el-form-item>
          <el-form-item label="公告内容">
            <el-input
              type="textarea"
              v-model="announcement.content"
              placeholder="请输入公告内容"
              rows="20"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <template #footer>
        <div class="flex justify-end">
          <el-button type="primary" size="large" @click="submitAnnouncement"
            >发布公告</el-button
          >
        </div>
      </template>
    </el-card>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { ApiNotice } from "@/core/api/index.js";
import Swal from "sweetalert2";

const announcement = ref({
  name: "",
  type: "",
  display: "首页公告区",
  notification: false,
  content: "",
});

const options = [
  {
    value: "学术公告",
    label: "学术公告",
  },
  {
    value: "考试公告",
    label: "考试公告",
  },
  {
    value: "招生与就业公告",
    label: "招生与就业公告",
  },
  {
    value: "行政管理公告",
    label: "行政管理公告",
  },
  {
    value: "学生事务公告",
    label: "学生事务公告",
  },
  {
    value: "后勤服务公告",
    label: "后勤服务公告",
  },
  {
    value: "节假日与休假安排公告",
    label: "节假日与休假安排公告",
  },
];

const submitAnnouncement = async () => {
  const response = await ApiNotice.publishNotice({
    mContent: announcement.value.content,
    mType: announcement.value.type,
    mTitle: announcement.value.name,
  });
  if (response.code == 0) {
    Swal.fire({
      title: "发布成功",
      text: `您已成功发布公告《${announcement.value.name}》！`,
      icon: "success",
    });
    initAnnouncement();
  }
};

const initAnnouncement = () => {
  announcement.value = {
    name: "",
    type: "",
    display: "首页公告区",
    notification: false,
    content: "",
  };
};
</script>
